import { _decorator, Node } from 'cc';
import { BaseView } from '../../../scripts/libs/rxjs/cc3/BaseView';
import { Timer } from '../../../scripts/libs/rxjs/cc3/CreatorSchedule';
import { v3 } from 'cc';
import { CaidaiHelper } from '../../scripts/CaidaiHelper';
import { Caidai } from './Caidai';
const { ccclass, property } = _decorator;

@ccclass('BurstComponent')
export class BurstComponent extends BaseView {

    @property(Node)
    targetNode: Node = null;

    start() {
        this.burst();
    }

    recycleCaidai() {
        this.node.children.map(_ => _).forEach(_ => {
            if (_.name != 'targetNode') {
                CaidaiHelper.PutCaidai(_);
            }
        });
    }

    _v30 = v3();
    _v31 = v3();

    @property
    delay: number = 0;

    burst() {
        this._v30.set(this.node.worldPosition);
        this._v30.z += 600;
        this._v31.set(this.targetNode.worldPosition);
        this._v31.z += 600;
        Timer(0 + this.delay, this.node).subscribe(_ => {
            CaidaiHelper.BurstCaidai(this.node, 75, this._v30, this._v31);
        }).addTo(this.node);
        Timer(.05 + this.delay, this.node).subscribe(_ => {
            CaidaiHelper.BurstCaidai(this.node, 45, this._v30, this._v31);
        }).addTo(this.node);
        Timer(.1 + this.delay, this.node).subscribe(_ => {
            CaidaiHelper.BurstCaidai(this.node, 45, this._v30, this._v31);
        }).addTo(this.node);
    }

    recycle() {
        let list = this.node.children.filter(_ => _.name != 'target');
        list.forEach(_ => {
            _.getComponent(Caidai).endCallback = null;
            CaidaiHelper.PutCaidai(_);
        });
    }
}

